package com.zjsru.oneDay;

/**
 *
 * Bigram分词
 * 给出第一个词first 和第二个词second，考虑在某些文本text中可能以 "first second third" 形式出现的情况，
 * 其中second紧随first出现，third紧随second出现。
 *
 * 对于每种这样的情况，将第三个词 "third" 添加到答案中，并返回答案
 *
 *
 * 输入：text = "alice is a good girl she is a good student", first = "a", second = "good"
 * 输出：["girl","student"]
 *
 * **/
public class findOcurrences {
    public String[] findOcurrences(String text, String first, String second) {
        String[] words = text.split(" ");
        int n = words.length;
        String[] result = new String[n];
        int l = 0;
        for (int i = 0; i < n; i++) {
            if(i +2 < n){
                if(words[i].equals(first) && words[i+1].equals(second)){
                    result[l] = words[i+2];
                    l++;
                }
            }
        }
        String[] ret = new String[l];//l =2 即数组包含两个元素
        for (int i = 0; i < l; i++) {
                ret[i] = result[i];
        }
        return  ret;

//        String[] words = text.split(" ");
//        List<String> list = new ArrayList<String>();
//        for (int i = 2; i < words.length; i++) {
//            if (words[i - 2].equals(first) && words[i - 1].equals(second)) {
//                list.add(words[i]);
//            }
//        }
//        int size = list.size();
//        String[] ret = new String[size];
//        for (int i = 0; i < size; i++) {
//            ret[i] = list.get(i);
//        }
//        return ret;
    }
    public static void main(String[] args){
        String test = "alice is a good girl she is a good student";
        String first = "a";
        String sceond = "good";
        findOcurrences findOcurrences = new findOcurrences();
        System.out.println(findOcurrences.findOcurrences(test,first,sceond));
    }
}
